package nbcondominios

class CuotasController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def beforeInterceptor = [action:this.&auth,
        except:['index', 'consulta' ]]
    def auth() {
        if(!session.user) {
            flash.message = "Disculpe, debe iniciar sesion en el sistema"
            redirect(controller:"user", action:"login")
            return false
        }
        if(!session.user.admin){
            flash.message = "Disculpe, esta tarea es solo para administradores"
            redirect(controller:"user", action:"login")
            return false
        }
    }
    
    def index = {
        if(!session.user) {
            flash.message = "Disculpe, debe iniciar sesion en el sistema"
            redirect(controller:"user", action:"login")
        } else {
            redirect(action: "list", params: params)
        }
    }

    def list = {
        if(!session.user) {
            flash.message = "Disculpe, debe iniciar sesion en el sistema"
            redirect(controller:"user", action:"login")
        } else {
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [cuotasInstanceList: Cuotas.list(params), cuotasInstanceTotal: Cuotas.count()]
        }
    }
    
    def consulta = {
        if(!session.user) {
            flash.message = "Disculpe, debe iniciar sesion en el sistema"
            redirect(controller:"user", action:"login")
        } else {
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [cuotasInstanceList: Cuotas.list(params), cuotasInstanceTotal: Cuotas.count()]
        }
    }
    
    def create = {
        def cuotasInstance = new Cuotas()
        cuotasInstance.properties = params
        return [cuotasInstance: cuotasInstance]
    }

    def save = {
        def cuotasInstance = new Cuotas(params)
        if (cuotasInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), cuotasInstance.id])}"
            redirect(action: "show", id: cuotasInstance.id)
        }
        else {
            render(view: "create", model: [cuotasInstance: cuotasInstance])
        }
    }

    def show = {
        def cuotasInstance = Cuotas.get(params.id)
        if (!cuotasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])}"
            redirect(action: "list")
        }
        else {
            [cuotasInstance: cuotasInstance]
        }
    }

    def edit = {
        def cuotasInstance = Cuotas.get(params.id)
        if (!cuotasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [cuotasInstance: cuotasInstance]
        }
    }

    def update = {
        def cuotasInstance = Cuotas.get(params.id)
        if (cuotasInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (cuotasInstance.version > version) {
                    
                    cuotasInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'cuotas.label', default: 'Cuotas')] as Object[], "Another user has updated this Cuotas while you were editing")
                    render(view: "edit", model: [cuotasInstance: cuotasInstance])
                    return
                }
            }
            cuotasInstance.properties = params
            if (!cuotasInstance.hasErrors() && cuotasInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), cuotasInstance.id])}"
                redirect(action: "show", id: cuotasInstance.id)
            }
            else {
                render(view: "edit", model: [cuotasInstance: cuotasInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def cuotasInstance = Cuotas.get(params.id)
        if (cuotasInstance) {
            try {
                cuotasInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cuotas.label', default: 'Cuotas'), params.id])}"
            redirect(action: "list")
        }
    }
}
